{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Importing VTK data to be displayed as Mesh\n",
    "\n",
    "Additional requirements for this example: `vtk`\n",
    "\n",
    "We will also see how to do some processing with `vtk` and visualize its results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import os\n",
    "import vtk\n",
    "import numpy as np\n",
    "\n",
    "from k3d.helpers import download\n",
    "\n",
    "filename = download('https://raw.githubusercontent.com/naucoin/VTKData/master/Data/cow.vtp')\n",
    "\n",
    "model_matrix = (\n",
    "     1.0,  0.0, 0.0, 0.0,\n",
    "     0.0,  0.0, 1.0, 0.0,\n",
    "     0.0,  1.0, 0.0, 0.0,\n",
    "     0.0,  0.0, 0.0, 1.0\n",
    ")\n",
    "\n",
    "reader = vtk.vtkXMLPolyDataReader()\n",
    "reader.SetFileName(filename)\n",
    "reader.Update()\n",
    "\n",
    "plot = k3d.plot()\n",
    "cow3d = k3d.vtk_poly_data(reader.GetOutput(), color=0xff0000, model_matrix=model_matrix)\n",
    "plot += cow3d\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('./output.html', 'w', encoding='UTF-8')\n",
    "f.write(plot.get_snapshot(9))\n",
    "f.close()\n",
    "\n",
    "plot.snapshot_include_js = False\n",
    "f = open('./smaller_output.html', 'w', encoding='UTF-8')\n",
    "f.write(plot.get_snapshot(9))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "print(os.stat('./output.html').st_size / 1024)\n",
    "print(os.stat('./smaller_output.html').st_size / 1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cow3d.wireframe = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cow3d.wireframe = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plane=vtk.vtkPlane()\n",
    "plane.SetOrigin(0,0,0)\n",
    "plane.SetNormal(1,1,0)\n",
    " \n",
    "cutter=vtk.vtkCutter()\n",
    "cutter.SetCutFunction(plane)\n",
    "cutter.SetInputConnection(reader.GetOutputPort())\n",
    "cutter.Update()\n",
    "\n",
    "FeatureEdges = vtk.vtkFeatureEdges()\n",
    "FeatureEdges.SetInputConnection(cutter.GetOutputPort())\n",
    "FeatureEdges.BoundaryEdgesOn()\n",
    "FeatureEdges.FeatureEdgesOff()\n",
    "FeatureEdges.NonManifoldEdgesOff()\n",
    "FeatureEdges.ManifoldEdgesOff()\n",
    "FeatureEdges.Update()\n",
    "\n",
    "cutStrips = vtk.vtkStripper() ; #Forms loops (closed polylines) from cutter\n",
    "cutStrips.SetInputConnection(cutter.GetOutputPort())\n",
    "cutStrips.Update()\n",
    "\n",
    "cutPoly = vtk.vtkPolyData() ; #This trick defines polygons as polyline loop\n",
    "cutPoly.SetPoints((cutStrips.GetOutput()).GetPoints())\n",
    "cutPoly.SetPolys((cutStrips.GetOutput()).GetLines())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_matrix = (\n",
    "     1.0,  0.0, 0.0, 0.0,\n",
    "     0.0,  0.0, 1.0, -5.0,\n",
    "     0.0,  1.0, 0.0, 0.0,\n",
    "     0.0,  0.0, 0.0, 1.0\n",
    ")\n",
    "plot += k3d.vtk_poly_data(cutPoly, color=0x0000ff,  model_matrix=model_matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
